package com.pur.zsms.ordercustommanage.web;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.lockmanage.service.ILockTransService;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.po.ShoppingCartMaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.note.Note;
import com.pur.zsms.note.NoteForSup;
import com.pur.zsms.note.service.INoteService;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ICustMaterDetailedOrderService;
import com.pur.zsms.ordercustommanage.service.ICustMaterOrderService;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.service.InsertCusFoodService;
import com.pur.zsms.ordercustommanage.service.impl.CustMaterOrderServiceImpl;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderStateVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVoFooter;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodList;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserLevelPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.quotation.service.IQuotationService;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierUserPo;
import com.pur.zsms.supplier.service.ISupplierOrderService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.DeleteSupplierVo;
import com.pur.zsms.supplier.vo.DeleteSupplierVoList;
import com.pur.zsms.supplier.vo.SupplierUserVo;
import com.pur.zsms.timecontrol.service.ITimeControlService;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.weixin.service.IWeixinService;

@Controller
@RequestMapping(value = "/order")
public class OrderCustControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(OrderCustControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "weixinService")
	private IWeixinService weixinService;

	@Resource(name = "lockTransService")
	private ILockTransService lockTransService;

	@Resource(name = "quotationService")
	private IQuotationService quotationService;

	@Resource(name = "selectFoodService")
	private ISelectFoodService selectFoodService;

	@Resource(name = "insertCusFoodService")
	private InsertCusFoodService insertCusFoodService;

	@Resource(name = "custMaterOrderService")
	private ICustMaterOrderService custMaterOrderService;

	@Resource(name = "custMaterDetailedOrderService")
	private ICustMaterDetailedOrderService custMaterDetailedOrderService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "supplierOrderService")
	private ISupplierOrderService supplierOrderService;

	@Resource(name = "unusualService")
	private IUnusualService unusualService;

	@Resource(name = "storeService")
	private IStoreService storeService;

	@Resource(name = "timeControlService")
	private ITimeControlService timeControlService;

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "noteService")
	private INoteService noteService;

	@Resource(name = "userService")
	private IUserService userService;

	@RequestMapping(value = "/commitOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitOrder(SelectFoodVo selectFoodVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");

			// 增加对时间的校验
			boolean flagTime = false;
			flagTime = timeControlService.timeControl(userInfo.getUserId(), selectFoodVo.getOrderDate());
			if (!flagTime) {
				// response.setContentType("text/html");
				json1.put("msg", "msg002");
				json1.put("success", false);
				return json1.toString();
			}

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectFoodVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectFoodVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上

			JSONArray array = JSONArray.fromObject(selectFoodVo.getFooddategridList());

			List<SelectFoodVo> selectFoodVoList = new ArrayList<SelectFoodVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				SelectFoodVo tselectFoodVo = new SelectFoodVo();
				tselectFoodVo.setFoodId(Integer.valueOf(object.get("foodId").toString()));
				tselectFoodVo.setFoodNum(Integer.valueOf(object.get("foodNum").toString()));
				tselectFoodVo.setOrderDate(selectFoodVo.getOrderDate());
				tselectFoodVo.setOrderDay(selectFoodVo.getOrderDay());

				selectFoodVoList.add(tselectFoodVo);

			}

			// 增加校验 校验这个菜品 中的材料是否 满足 此店长对应供货商 ----下
			boolean checkflag = selectFoodService.checkMaterIsInUseSup(selectFoodVoList, userInfo);
			if (!checkflag) {
				json1.put("success", false);
				json1.put("msg", selectFoodService.getMsg());
				return json1.toString();
			}
			// 增加校验 校验这个菜品 中的材料是否 满足 此店长对应供货商 ----上

			int ordId = selectFoodService.addOrder(selectFoodVoList, userInfo);
			insertCusFoodService.addOrderFood(selectFoodVoList, userInfo, ordId);
			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/*
	 * @RequestMapping(value = "/deleteFood.do", method = { RequestMethod.POST
	 * })
	 * 
	 * @ResponseBody public String deleteFood(List<ExcelVo> zzy) {
	 * 
	 * JSONObject json1 = new JSONObject(); try {
	 * 
	 * json1.put("success", true); } catch (Exception e) { e.printStackTrace();
	 * json1.put("success", false); json1.put("msg", e.getMessage()); } return
	 * json1.toString(); }
	 */

	/**
	 * 选择材料
	 * */
	@RequestMapping(value = "/commitMaterOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderList(SelectMaterVo selectMaterVo, HttpSession session) throws Exception {

		List<String> supIdarray = new ArrayList<String>();// 微信提醒用的供货商列表
		JSONObject json1 = new JSONObject();
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();

		// 供货商短信提醒
		List<OrderSupplierMaterPo> noteOrderSupplierMaterPoList = new ArrayList<OrderSupplierMaterPo>();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = 0;
		try {

			shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

			// 事物锁
			boolean lockFlag = lockTransService.lockTransForCust("commitMaterOrder.do", shopId);
			if (!lockFlag) {
				json1.put("msg", "正在提交请等待！");
				json1.put("success", false);
				return json1.toString();
			}

			// 增加对时间的校验
			boolean flagTime = false;

			flagTime = timeControlService.timeControl(userInfo.getUserId(), selectMaterVo.getOrderDate());

			if (!flagTime) {
				// response.setContentType("text/html");
				json1.put("success", false);
				json1.put("msg", "msg001");

				return json1.toString();
			}
			int ordId = 0;
			// 增加校验 每天只能提交一次采购清单-----------下
			/*
			 * ordId =
			 * selectFoodService.checkCommitMaterOrder(selectMaterVo.getOrderDate
			 * (), shopId, 0); if (ordId != 0) { json1.put("success", false);
			 * json1.put("msg", selectFoodService.getMsg()); return
			 * json1.toString(); }
			 */
			// 增加校验 每天只能提交一次采购清单-----------上

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectMaterVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectMaterVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上

			// 从购物车中取出 商品 --zhouzyi
			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();

			List<ShoppingCartMaterPo> shoppingCartMaterPoList = materService.getShoppingCartMaterPo(shopId);
			for (ShoppingCartMaterPo shoppingCartMaterPo : shoppingCartMaterPoList) {
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(shoppingCartMaterPo.getMaterId());
				tselectMaterVo.setMaterNum(shoppingCartMaterPo.getMaterNum());
				tselectMaterVo.setMaterLevel(shoppingCartMaterPo.getMaterLevel());
				tselectMaterVo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed());
				tselectMaterVo.setLeaveMessage(shoppingCartMaterPo.getLeaveMessage());
				tselectMaterVo.setSupId(shoppingCartMaterPo.getSupId());

				supIdarray.add(String.valueOf(shoppingCartMaterPo.getSupId()));// 微信通知所需的供货商

				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());
				tselectMaterVo.setSubmitOrdTime(nowTime);
				selectMaterVoList.add(tselectMaterVo);

				// 真实 材料表
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(shoppingCartMaterPo.getMaterId());
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(shoppingCartMaterPo.getMaterNum());
				// 直接选择材料不设置 需要重量表 ，因为不会自动产生出库订单
				/*
				 * // 需要的重量 tOrderSupplierMaterRealVo .setOrdMaterWeight(new
				 * BigDecimal(object.get("materNum").toString()));
				 */
				tOrderSupplierMaterRealVo.setOrdMaterLevel(shoppingCartMaterPo.getMaterLevel());
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(shoppingCartMaterPo.getSupId());
				// ordSupId = object.get("supId").toString();
				// 订单id

				// 检索周期报价
				String materValueUnit = quotationService.getCompanyPeriodValue(shoppingCartMaterPo.getMaterId(), shoppingCartMaterPo.getSupId(),
						shopId);
				if (!"0".equals(materValueUnit)) {
					tOrderSupplierMaterRealVo.setOrdMaterValueUnit(new BigDecimal(materValueUnit));
					tOrderSupplierMaterRealVo.setPeriodValueFlag("1");
				} else {
					tOrderSupplierMaterRealVo.setPeriodValueFlag("0");
				}
				tOrderSupplierMaterRealVo.setOrdId(ordId);
				tOrderSupplierMaterRealVo.setOrdCustId((int) shopId);
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);
			}

			// 往客户订单表中插入值
			OrderCustPo orderCustPo = selectFoodService.addMaterOrder(selectMaterVoList, shopId);
			ordId = orderCustPo.getOrdId();
			insertCusFoodService.addMaterOrderFood(selectMaterVoList, ordId);
			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			/*
			 * OrderCustVo orderCustVo = new OrderCustVo(); orderCustVo =
			 * selectFoodService.getOrderCust(String.valueOf(ordId));
			 * selectFoodService.passMaterOrder(orderCustVo);
			 */
			for (int i = 0; i < selectMaterVoList.size(); i++) {

				// List<FoodMaterPo> foodMaterPoList =
				// selectFoodService.getFootMaterByFoodId(orderCustFoodVoList.get(i).getOrdFoodId());
				// for (int j=0 ; j< selectMaterVoList.size() ;j++){

				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
				orderSupplierMaterPo.setOrdId(ordId);
				orderSupplierMaterPo.setOrdFoodId(0);
				orderSupplierMaterPo.setOrdMaterId(selectMaterVoList.get(i).getMaterId());
				orderSupplierMaterPo.setLeaveMessage(selectMaterVoList.get(i).getLeaveMessage());

				// 食材 价格 重量信息

				orderSupplierMaterPo.setOrdMaterWeight(selectMaterVoList.get(i).getMaterNum());
				orderSupplierMaterPo.setOrdMaterValue(selectMaterVoList.get(i).getMaterValueUndressed());

				// 通过供货商关系表取得供货商
				orderSupplierMaterPo.setOrdSupId(selectMaterVoList.get(i).getSupId());

				orderSupplierMaterPo.setOrdMaterLevel(selectMaterVoList.get(i).getMaterLevel());
				orderSupplierMaterPo.setSubmitOrdTime(selectMaterVoList.get(i).getSubmitOrdTime());
				// 插入供货生附表信息
				selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				noteOrderSupplierMaterPoList.add(orderSupplierMaterPo);
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(String.valueOf(ordId));
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustPo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustPo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustPo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustPo.getOrdId());
				orderSupplierPo.setOrdState(3);
				orderSupplierPo.setOrdPaymentState(0);// 正常结款状态
				orderSupplierPo.setCommitDeliverState(0);// 提交实际送货重量的状态 未提交

				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				selectFoodService.addOrderSupplier(orderSupplierPo);
			}

			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList, ordId);
			/*
			 * if (flag) { OrderSupplierPo orderSupplierPo = new
			 * OrderSupplierPo(); orderSupplierPo =
			 * supplierOrderService.getOrderSupByOrdIdSupId(ordId, ordSupId);
			 * supplierOrderService.updateOrderSupPoState(orderSupplierPo, 3);
			 * json1.put("success", true); } else { json1.put("success", false);
			 * json1.put("msg", selectFoodService.getMsg()); return
			 * json1.toString(); }
			 */

			// 删除购物车中的商品
			materService.deleteShoppingCartForCust(shopId);
			noteService.sendMessageToPurchase(shopService.getShopPo(shopId).getShopName() + "刚新增了订单！订单号为" + ordId, "18202559101");

			noteService.sendMessageToSup(String.valueOf(ordId), selectMaterVo.getOrderDate(), shopService.getShopPo(shopId).getShopName());
			// 微信提醒

			String supId_weixin = org.apache.commons.lang.StringUtils.join(supIdarray.toArray(), ",");
			List<OrderSupplierPo> listOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(String.valueOf(ordId), supId_weixin);
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderList("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}

			}
			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		} finally {
			// 解锁
			lockTransService.unlockTransForCust("commitMaterOrder.do", shopId);
		}
		return json1.toString();
	}

	/**
	 * 选择材料 自采
	 * */
	@RequestMapping(value = "/commitMaterOrderListSelf.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderListSelf(SelectMaterVo selectMaterVo, HttpSession session) {

		JSONObject json1 = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		try {

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectMaterVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectMaterVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上
			JSONArray array = JSONArray.fromObject(selectMaterVo.getMaterdategridList());

			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tselectMaterVo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tselectMaterVo.setMaterValueAll(new BigDecimal(object.get("materValueAll").toString()));
				tselectMaterVo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());

				selectMaterVoList.add(tselectMaterVo);

			}

			// 往客户订单表中插入值
			int ordId = selectFoodService.addMaterOrderForSelfForunusual(selectMaterVoList, userInfo);
			insertCusFoodService.addMaterOrderFoodForSelfForunusual(selectMaterVoList, userInfo, ordId);
			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			OrderCustVo orderCustVo = new OrderCustVo();
			orderCustVo = selectFoodService.getOrderCust(String.valueOf(ordId));
			List<OrderCustFoodVo> orderCustFoodVoList = null;
			orderCustFoodVoList = selectFoodService.getOrderCustFood(String.valueOf(ordId));
			for (int i = 0; i < orderCustFoodVoList.size(); i++) {

				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
				orderSupplierMaterPo.setOrdId(ordId);
				orderSupplierMaterPo.setOrdFoodId(0);
				orderSupplierMaterPo.setOrdMaterId(orderCustFoodVoList.get(i).getOrdMaterId());

				// 食材 价格 重量信息

				orderSupplierMaterPo.setOrdMaterWeight(orderCustFoodVoList.get(i).getOrdMaterNum());
				orderSupplierMaterPo.setOrdMaterValue(orderCustFoodVoList.get(i).getOrdMaterValue());

				// 供货商为0 为自采
				orderSupplierMaterPo.setOrdSupId(0);

				orderSupplierMaterPo.setOrdMaterLevel(orderCustFoodVoList.get(i).getOrdMaterLevel());
				// 插入供货生附表信息
				selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				// }
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(String.valueOf(ordId));
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustVo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustVo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustVo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustVo.getOrdId());
				orderSupplierPo.setOrdState(5);// 收货
				orderSupplierPo.setOrdPaymentState(2);// 正常结款状态
				orderSupplierPo.setCommitDeliverState(1);// 提交实际送货重量的状态 未提交

				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				orderSupplierPo.setOrdValueReal(new BigDecimal(a[1].toString()));
				selectFoodService.addOrderSupplier(orderSupplierPo);

			}

			// 插入到真实需要的表中
			JSONArray arrayReal = JSONArray.fromObject(selectMaterVo.getMaterdategridList());
			// String ordSupId = "";
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();
			for (int i = 0; i < arrayReal.size(); i++) {
				JSONObject object = JSONObject.fromObject(arrayReal.get(i));
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("materId").toString()));
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(object.get("materNum").toString()));

				// 直接选择材料不设置 需要重量表 ，因为不会自动产生出库订单
				/*
				 * // 需要的重量 tOrderSupplierMaterRealVo .setOrdMaterWeight(new
				 * BigDecimal(object.get("materNum").toString()));
				 */

				tOrderSupplierMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(0);
				// ordSupId = object.get("supId").toString();
				// 订单id
				tOrderSupplierMaterRealVo.setOrdId(ordId);
				// 订单价格
				tOrderSupplierMaterRealVo.setOrdMaterValue(new BigDecimal(object.get("materValueAll").toString()));
				tOrderSupplierMaterRealVo.setOrdCustId((int) userInfo.getUserId());
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);

			}

			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList);
			/*
			 * if (flag) { OrderSupplierPo orderSupplierPo = new
			 * OrderSupplierPo(); orderSupplierPo =
			 * supplierOrderService.getOrderSupByOrdIdSupId(ordId, ordSupId);
			 * supplierOrderService.updateOrderSupPoState(orderSupplierPo, 3);
			 * json1.put("success", true); } else { json1.put("success", false);
			 * json1.put("msg", selectFoodService.getMsg()); return
			 * json1.toString(); }
			 */

			// 异常订单处理
			if (selectMaterVo.getUnusualDeal() != 0) {
				unusualService.orderUnusualDeal(selectMaterVo, ordId);

			}

			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/**
	 * 异常处理集中采购
	 * */
	@RequestMapping(value = "/commitMaterOrderCon.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderListCon(SelectMaterVo selectMaterVo, HttpSession session) {

		JSONObject json1 = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		try {
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectMaterVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectMaterVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上
			JSONArray array = JSONArray.fromObject(selectMaterVo.getMaterdategridList());

			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tselectMaterVo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tselectMaterVo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());
				// 增加异常处理属性
				tselectMaterVo.setUnusualDeal(selectMaterVo.getUnusualDeal());
				selectMaterVoList.add(tselectMaterVo);

			}

			// 往客户订单表中插入值
			OrderCustPo orderCustPo = selectFoodService.addMaterOrder(selectMaterVoList, shopId);
			int ordId = orderCustPo.getOrdId();
			insertCusFoodService.addMaterOrderFood(selectMaterVoList, ordId);
			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			/*
			 * OrderCustVo orderCustVo = new OrderCustVo(); orderCustVo =
			 * selectFoodService.getOrderCust(String.valueOf(ordId));
			 * selectFoodService.passMaterOrder(orderCustVo);
			 */
			List<OrderCustFoodVo> orderCustFoodVoList = null;
			orderCustFoodVoList = selectFoodService.getOrderCustFood(String.valueOf(ordId));
			for (int i = 0; i < orderCustFoodVoList.size(); i++) {

				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
				orderSupplierMaterPo.setOrdId(ordId);
				orderSupplierMaterPo.setOrdFoodId(0);
				orderSupplierMaterPo.setOrdMaterId(orderCustFoodVoList.get(i).getOrdMaterId());

				// 食材 价格 重量信息

				orderSupplierMaterPo.setOrdMaterWeight(orderCustFoodVoList.get(i).getOrdMaterNum());
				orderSupplierMaterPo.setOrdMaterValue(orderCustFoodVoList.get(i).getOrdMaterValue());

				// 通过供货商关系表取得供货商
				MaterPo materPo = selectFoodService.getMaterValueByMaterId(orderCustFoodVoList.get(i).getOrdMaterId());
				ShopSupplierTypePo suppCusPo = selectFoodService.getSuppCusPoByMaterType(materPo.getMaterType(), orderCustPo.getOrdCustId());
				orderSupplierMaterPo.setOrdSupId(suppCusPo.getSupId());

				orderSupplierMaterPo.setOrdMaterLevel(orderCustFoodVoList.get(i).getOrdMaterLevel());
				// 插入供货生附表信息
				selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				// }
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(String.valueOf(ordId));
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustPo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustPo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustPo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustPo.getOrdId());
				orderSupplierPo.setOrdState(3);
				orderSupplierPo.setOrdPaymentState(0);// 正常结款状态
				orderSupplierPo.setCommitDeliverState(0);// 提交实际送货重量的状态 未提交

				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				selectFoodService.addOrderSupplier(orderSupplierPo);

			}

			// 插入到真实需要的表中
			JSONArray arrayReal = JSONArray.fromObject(selectMaterVo.getMaterdategridList());
			// String ordSupId = "";
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();
			for (int i = 0; i < arrayReal.size(); i++) {
				JSONObject object = JSONObject.fromObject(arrayReal.get(i));
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("materId").toString()));
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(object.get("materNum").toString()));

				tOrderSupplierMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(Integer.valueOf(object.get("supId").toString()));
				// ordSupId = object.get("supId").toString();
				// 订单id
				tOrderSupplierMaterRealVo.setOrdId(ordId);
				tOrderSupplierMaterRealVo.setOrdCustId((int) userInfo.getUserId());
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);

			}

			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList);

			// 异常订单处理
			if (selectMaterVo.getUnusualDeal() != 0) {
				unusualService.orderUnusualDeal(selectMaterVo, ordId);
			}

			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/preUpdateSelectMater.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateSelectMater(SelectMaterVo selectMaterVo, HttpSession session) {
		String mobile = session.getAttribute("mobile").toString();
		int buySupplier = selectMaterVo.getBuySupplier();
		try {
			selectMaterVo = selectFoodService.getOrderCustForUpdateSelect(selectMaterVo.getOrdId());
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("selectMaterVo", selectMaterVo);
		if (mobile.equals("open")) {
			if (selectMaterVo.getBuySelfFlag() == 0) {
				return new ModelAndView("/jsp-mobile/select/selectMaterForUpdate_mobile", map);
			} else {
				return new ModelAndView("/jsp-mobile/select/selectMaterForUpdate_mobile_self", map);
			}
		} else {
			if (buySupplier == 1) {
				UserPo userInfo = (UserPo) session.getAttribute("user");
				long supId;
				try {
					supId = supplierService.getSupIdByUserId(userInfo.getUserId());
					map.put("supId", supId);
				} catch (GenericException e) {
					e.printStackTrace();
				}

				return new ModelAndView("/selectfood/selectMaterForSupplier", map);
			}

			if (selectMaterVo.getBuySelfFlag() == 0) {
				return new ModelAndView("/selectfood/selectMaterForUpdate", map);
			} else {
				return new ModelAndView("/selectfood/selectMaterForUpdate_self", map);
			}
		}

	}

	@RequestMapping(value = "/preSelectMaterForSup.do", method = { RequestMethod.GET })
	public ModelAndView preSelectMaterForSup(SelectMaterVo selectMaterVo, HttpSession session) {
		UserPo userInfo = (UserPo) session.getAttribute("user");
		Map map = new HashMap();
		try {
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			selectMaterVo.setSupId(supId);
			selectMaterVo.setOrdCustName(shopService.getShopPo(selectMaterVo.getOrdCustId()).getShopName());
			map.put("selectMaterVo", selectMaterVo);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		return new ModelAndView("/selectfood/selectMaterForSupNew", map);

	}

	/**
	 * 选择材料
	 * */
	@RequestMapping(value = "/commitMaterOrderForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderListForSup(SelectMaterVo selectMaterVo, HttpSession session) {

		UserPo userInfo = (UserPo) session.getAttribute("user");
		JSONObject json1 = new JSONObject();
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();

		// 供货商短信提醒
		List<OrderSupplierMaterPo> noteOrderSupplierMaterPoList = new ArrayList<OrderSupplierMaterPo>();

		try {
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			selectMaterVo.setSupId(supId);
			selectMaterVo.setOrdSupId(supId);
			long shopId = selectMaterVo.getOrdCustId();

			// 增加校验 每天只能提交一次采购清单-----------下
			int ordId = 0;
			/*
			 * int ordId =
			 * selectFoodService.checkCommitMaterOrderForSup(selectMaterVo
			 * .getOrderDate(), shopId, supId); if (ordId != 0) {
			 * json1.put("success", false); json1.put("msg",
			 * selectFoodService.getMsg()); return json1.toString(); }
			 */
			// 增加校验 每天只能提交一次采购清单-----------上

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectMaterVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectMaterVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上
			JSONArray array = JSONArray.fromObject(selectMaterVo.getMaterdategridList());

			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tselectMaterVo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tselectMaterVo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tselectMaterVo.setMaterValue(new BigDecimal(object.get("materValue").toString()));

				tselectMaterVo.setLeaveMessage(object.get("leaveMessage").toString());
				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());
				tselectMaterVo.setSubmitOrdTime(nowTime);
				selectMaterVoList.add(tselectMaterVo);

			}
			// 先判断 存在不存在此供货商的ordID

			// 往客户订单表中插入值
			// ordId =
			// selectFoodService.checkCommitMaterOrder(selectMaterVo.getOrderDate(),
			// shopId, 0);
			if (ordId == 0) {
				OrderCustPo orderCustPo = selectFoodService.addMaterOrder(selectMaterVoList, shopId);
				ordId = orderCustPo.getOrdId();
				insertCusFoodService.addMaterOrderFood(selectMaterVoList, ordId);
			} else {
				selectMaterVo.setOrdId(ordId);
				selectFoodService.updateMaterOrder(selectMaterVoList, ordId);
				insertCusFoodService.updateMaterOrderFood(selectMaterVoList, ordId);
			}
			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			/*
			 * OrderCustVo orderCustVo = new OrderCustVo(); orderCustVo =
			 * selectFoodService.getOrderCust(String.valueOf(ordId));
			 * selectFoodService.passMaterOrder(orderCustVo);
			 */
			// List<OrderCustFoodVo> orderCustFoodVoList = null;
			// orderCustFoodVoList =
			// selectFoodService.getOrderCustFood(String.valueOf(ordId));
			for (int i = 0; i < selectMaterVoList.size(); i++) {
				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
				orderSupplierMaterPo.setOrdId(ordId);
				orderSupplierMaterPo.setOrdFoodId(0);
				orderSupplierMaterPo.setOrdMaterId(selectMaterVoList.get(i).getMaterId());
				orderSupplierMaterPo.setLeaveMessage(selectMaterVoList.get(i).getLeaveMessage());

				// 食材 价格 重量信息

				orderSupplierMaterPo.setOrdMaterWeight(selectMaterVoList.get(i).getMaterNum());
				orderSupplierMaterPo.setOrdMaterValue(selectMaterVoList.get(i).getMaterValue());

				// 通过供货商关系表取得供货商
				orderSupplierMaterPo.setOrdSupId(selectMaterVo.getSupId());

				orderSupplierMaterPo.setOrdMaterLevel(selectMaterVoList.get(i).getMaterLevel());
				orderSupplierMaterPo.setSubmitOrdTime(selectMaterVoList.get(i).getSubmitOrdTime());
				// 插入供货生附表信息
				selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				// noteOrderSupplierMaterPoList.add(orderSupplierMaterPo);

			}

			// ----生成材料订单的主表---
			OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
			// 检索当天的表中是否存在该供货商和客户的总表 ordersupplierPo

			orderSupplierPo.setOrdCustId(selectMaterVo.getOrdCustId());
			orderSupplierPo.setOrdDate(selectMaterVo.getOrderDate());
			orderSupplierPo.setOrdDay(selectMaterVo.getOrderDay());
			orderSupplierPo.setOrdId(ordId);
			orderSupplierPo.setOrdState(3);
			orderSupplierPo.setOrdPaymentState(0);// 正常结款状态
			orderSupplierPo.setCommitDeliverState(0);// 提交实际送货重量的状态 未提交
			orderSupplierPo.setOrdSupId(supId);
			selectFoodService.addOrderSupplier(orderSupplierPo);

			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdIdAndSupIdAndShopId(String.valueOf(ordId), supId, shopId);

			Object[] a = list.get(0);
			orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
			selectFoodService.updateOrderSupplier(orderSupplierPo);

			// 插入到真实需要的表中
			JSONArray arrayReal = JSONArray.fromObject(selectMaterVo.getMaterdategridList());
			// String ordSupId = "";
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();
			for (int i = 0; i < arrayReal.size(); i++) {
				JSONObject object = JSONObject.fromObject(arrayReal.get(i));
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("materId").toString()));
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(object.get("materNum").toString()));

				// 直接选择材料不设置 需要重量表 ，因为不会自动产生出库订单
				/*
				 * // 需要的重量 tOrderSupplierMaterRealVo .setOrdMaterWeight(new
				 * BigDecimal(object.get("materNum").toString()));
				 */

				tOrderSupplierMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(selectMaterVo.getSupId());
				// ordSupId = object.get("supId").toString();
				// 订单id

				// 检索周期报价
				String materValueUnit = quotationService.getCompanyPeriodValue(Integer.valueOf(object.get("materId").toString()),
						selectMaterVo.getSupId(), shopId);
				if (!"0".equals(materValueUnit)) {
					tOrderSupplierMaterRealVo.setOrdMaterValueUnit(new BigDecimal(materValueUnit));
					tOrderSupplierMaterRealVo.setPeriodValueFlag("1");
				} else {
					tOrderSupplierMaterRealVo.setPeriodValueFlag("0");
				}
				tOrderSupplierMaterRealVo.setOrdId(ordId);
				tOrderSupplierMaterRealVo.setOrdCustId((int) shopId);
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);

			}

			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList);
			/*
			 * if (flag) { OrderSupplierPo orderSupplierPo = new
			 * OrderSupplierPo(); orderSupplierPo =
			 * supplierOrderService.getOrderSupByOrdIdSupId(ordId, ordSupId);
			 * supplierOrderService.updateOrderSupPoState(orderSupplierPo, 3);
			 * json1.put("success", true); } else { json1.put("success", false);
			 * json1.put("msg", selectFoodService.getMsg()); return
			 * json1.toString(); }
			 */

			// 删除购物车中的商品
			materService.deleteShoppingCartSupAll(selectMaterVo.getSupId());
			noteService.sendMessageToPurchase(shopService.getShopPo(shopId).getShopName() + "刚新增了订单！订单号为" + ordId, "18202559101");

			noteService.sendMessageToSup(String.valueOf(ordId), selectMaterVo.getOrderDate(), shopService.getShopPo(shopId).getShopName());

			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/**
	 * 选择材料
	 * */
	@RequestMapping(value = "/commitMaterOrderForUpdate.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderForUpdate(SelectMaterVo selectMaterVo, HttpSession session) throws Exception {

		JSONObject json1 = new JSONObject();
		long shopId = 0;

		UserPo userInfo = (UserPo) session.getAttribute("user");
		try {
			List<String> supIdarray = new ArrayList<String>();// 微信提醒用的供货商列表

			shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

			// 事物锁
			boolean lockFlag = lockTransService.lockTransForCust("commitMaterOrderForUpdate.do", shopId);
			if (!lockFlag) {
				json1.put("msg", "正在提交请等待！");
				json1.put("success", false);
				return json1.toString();
			}

			// 增加对时间的校验
			boolean flagTime = false;

			flagTime = timeControlService.timeControl(userInfo.getUserId(), selectMaterVo.getOrderDate());

			if (!flagTime) {
				// response.setContentType("text/html");
				json1.put("success", false);
				json1.put("msg", "msg001");

				return json1.toString();
			}

			// 从购物车中取得商品
			// 从购物车中取出 商品 --zhouzyi
			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();

			List<ShoppingCartMaterPo> shoppingCartMaterPoList = materService.getShoppingCartMaterPo(shopId);
			for (ShoppingCartMaterPo shoppingCartMaterPo : shoppingCartMaterPoList) {
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(shoppingCartMaterPo.getMaterId());
				tselectMaterVo.setMaterNum(shoppingCartMaterPo.getMaterNum());
				tselectMaterVo.setMaterLevel(shoppingCartMaterPo.getMaterLevel());
				tselectMaterVo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed());
				tselectMaterVo.setLeaveMessage(shoppingCartMaterPo.getLeaveMessage());
				tselectMaterVo.setSupId(shoppingCartMaterPo.getSupId());

				supIdarray.add(String.valueOf(shoppingCartMaterPo.getSupId()));// 微信通知所需的供货商

				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());
				selectMaterVoList.add(tselectMaterVo);

				// 真实 材料表
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(shoppingCartMaterPo.getMaterId());
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(shoppingCartMaterPo.getMaterNum());
				// 直接选择材料不设置 需要重量表 ，因为不会自动产生出库订单
				/*
				 * // 需要的重量 tOrderSupplierMaterRealVo .setOrdMaterWeight(new
				 * BigDecimal(object.get("materNum").toString()));
				 */
				tOrderSupplierMaterRealVo.setOrdMaterLevel(shoppingCartMaterPo.getMaterLevel());
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(shoppingCartMaterPo.getSupId());
				
				// ordSupId = object.get("supId").toString();
				// 订单id

				// 检索周期报价
				String materValueUnit = quotationService.getCompanyPeriodValue(shoppingCartMaterPo.getMaterId(), shoppingCartMaterPo.getSupId(),
						shopId);
				if (!"0".equals(materValueUnit)) {
					tOrderSupplierMaterRealVo.setOrdMaterValueUnit(new BigDecimal(materValueUnit));
					tOrderSupplierMaterRealVo.setPeriodValueFlag("1");
				} else {
					tOrderSupplierMaterRealVo.setPeriodValueFlag("0");
				}
				tOrderSupplierMaterRealVo.setOrdId(selectMaterVo.getOrdId());
				tOrderSupplierMaterRealVo.setOrdCustId((int) shopId);
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);
			}

			// 往客户订单表中插入值
			selectFoodService.updateMaterOrder(selectMaterVoList, selectMaterVo.getOrdId());
			insertCusFoodService.updateMaterOrderFood(selectMaterVoList, selectMaterVo.getOrdId());

			// 删除供货商订单
			selectFoodService.deleteSupOrderByOid(selectMaterVo.getOrdId());

			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			OrderCustVo orderCustVo = new OrderCustVo();
			orderCustVo = selectFoodService.getOrderCust(String.valueOf(selectMaterVo.getOrdId()));
			/*
			 * List<OrderCustFoodVo> orderCustFoodVoList = null;
			 * orderCustFoodVoList = selectFoodService
			 * .getOrderCustFood(String.valueOf(selectMaterVo.getOrdId()));
			 */
			for (int i = 0; i < selectMaterVoList.size(); i++) {

				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();

				List<OrderSupplierMaterPo> tOrderSupplierMaterPoList = selectFoodService.getOrderSupplierMaterPoByMateridOrdId(
						selectMaterVoList.get(i).getMaterId(), selectMaterVo.getOrdId(), selectMaterVoList.get(i).getSupId());
				// 得到材料价格
				BigDecimal materValue = materService.getMaterLevelValue(selectMaterVoList.get(i).getMaterId());

				if (tOrderSupplierMaterPoList != null && tOrderSupplierMaterPoList.size() > 0) {
					orderSupplierMaterPo = tOrderSupplierMaterPoList.get(0);
					orderSupplierMaterPo.setOrdMaterWeight(selectMaterVoList.get(i).getMaterNum().add(orderSupplierMaterPo.getOrdMaterWeight()));
					orderSupplierMaterPo.setOrdMaterValue(orderSupplierMaterPo.getOrdMaterWeight().multiply(materValue));
					orderSupplierMaterPo.setLeaveMessage(selectMaterVoList.get(i).getLeaveMessage());
					orderSupplierMaterPo.setSubmitOrdTime(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());

				} else {
					orderSupplierMaterPo.setOrdId(selectMaterVo.getOrdId());
					orderSupplierMaterPo.setOrdFoodId(0);
					orderSupplierMaterPo.setOrdMaterId(selectMaterVoList.get(i).getMaterId());

					// 食材 价格 重量信息

					orderSupplierMaterPo.setOrdMaterWeight(selectMaterVoList.get(i).getMaterNum());
					orderSupplierMaterPo.setOrdMaterValue(materValue.multiply(selectMaterVoList.get(i).getMaterNum()));
					orderSupplierMaterPo.setOrdSupId(selectMaterVoList.get(i).getSupId());
					orderSupplierMaterPo.setOrdMaterLevel(selectMaterVoList.get(i).getMaterLevel());
					orderSupplierMaterPo.setLeaveMessage(selectMaterVoList.get(i).getLeaveMessage());
					orderSupplierMaterPo.setSubmitOrdTime(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());

				}

				// 插入供货生附表信息
				selectFoodService.updateOrderSupplierMater(orderSupplierMaterPo);
				// }
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(String.valueOf(selectMaterVo.getOrdId()));
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustVo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustVo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustVo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustVo.getOrdId());
				orderSupplierPo.setOrdState(3);
				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				selectFoodService.updateOrderSupplier(orderSupplierPo);

			}
			
			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterRealForUpdate(orderSupplierMaterRealVoList);
			// 删除购物车中的商品
			materService.deleteShoppingCartForCust((int) shopId);

			Note note = new Note(shopService.getShopPo(shopId).getShopName() + "刚修改了订单！订单号为" + selectMaterVo.getOrdId(), "18202559101");
			note.start();

			// 微信提醒
			String supId_weixin = org.apache.commons.lang.StringUtils.join(supIdarray.toArray(), ",");

			List<OrderSupplierPo> listOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(String.valueOf(selectMaterVo.getOrdId()),
					supId_weixin);
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderForUpdate("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}

			}
			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		} finally {
			// 解锁
			lockTransService.unlockTransForCust("commitMaterOrderForUpdate.do", shopId);
		}
		return json1.toString();
	}

	/**
	 * 选择材料
	 * */
	@RequestMapping(value = "/commitMaterOrderReplenish.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderReplenish(SelectMaterVo selectMaterVo, HttpSession session) {

		JSONObject json1 = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		try {
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			// 增加对时间的校验
			/*
			 * boolean flagTime = false;
			 * 
			 * flagTime = timeControlService.timeControl(userInfo.getUserId(),
			 * selectMaterVo.getOrderDate());
			 * 
			 * if (!flagTime) { // response.setContentType("text/html");
			 * json1.put("success", false); json1.put("msg", "msg001");
			 * 
			 * return json1.toString(); }
			 */

			// 增加校验 每天只能提交一次采购清单-----------下
			/*
			 * boolean checkflag =
			 * selectFoodService.checkCommitMaterOrder(selectMaterVo
			 * .getOrderDate(), userInfo.getUserId()); if (!checkflag) {
			 * json1.put("success", false); json1.put("msg",
			 * selectFoodService.getMsg()); return json1.toString(); }
			 */
			// 增加校验 每天只能提交一次采购清单-----------上

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(selectMaterVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			selectMaterVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上
			JSONArray array = JSONArray.fromObject(selectMaterVo.getMaterdategridList());

			List<SelectMaterVo> selectMaterVoList = new ArrayList<SelectMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				SelectMaterVo tselectMaterVo = new SelectMaterVo();
				tselectMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tselectMaterVo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tselectMaterVo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tselectMaterVo.setOrderDate(selectMaterVo.getOrderDate());
				tselectMaterVo.setOrderDay(selectMaterVo.getOrderDay());

				selectMaterVoList.add(tselectMaterVo);

			}

			// 往客户订单表中插入值
			OrderCustPo orderCustPo = selectFoodService.addMaterOrder(selectMaterVoList, shopId);
			int ordId = orderCustPo.getOrdId();
			insertCusFoodService.addMaterOrderFood(selectMaterVoList, ordId);
			// 往供应商订单表中插入值

			// 取得订单主表实体对象
			/*
			 * OrderCustVo orderCustVo = new OrderCustVo(); orderCustVo =
			 * selectFoodService.getOrderCust(String.valueOf(ordId));
			 * selectFoodService.passMaterOrder(orderCustVo);
			 */
			List<OrderCustFoodVo> orderCustFoodVoList = null;
			orderCustFoodVoList = selectFoodService.getOrderCustFood(String.valueOf(ordId));
			for (int i = 0; i < orderCustFoodVoList.size(); i++) {

				// List<FoodMaterPo> foodMaterPoList =
				// selectFoodService.getFootMaterByFoodId(orderCustFoodVoList.get(i).getOrdFoodId());
				// for (int j=0 ; j< selectMaterVoList.size() ;j++){

				OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
				orderSupplierMaterPo.setOrdId(ordId);
				orderSupplierMaterPo.setOrdFoodId(0);
				orderSupplierMaterPo.setOrdMaterId(orderCustFoodVoList.get(i).getOrdMaterId());

				// 食材 价格 重量信息

				orderSupplierMaterPo.setOrdMaterWeight(orderCustFoodVoList.get(i).getOrdMaterNum());
				orderSupplierMaterPo.setOrdMaterValue(orderCustFoodVoList.get(i).getOrdMaterValue());

				// 通过供货商关系表取得供货商
				MaterPo materPo = selectFoodService.getMaterValueByMaterId(orderCustFoodVoList.get(i).getOrdMaterId());
				ShopSupplierTypePo suppCusPo = selectFoodService.getSuppCusPoByMaterType(materPo.getMaterType(), orderCustPo.getOrdCustId());
				orderSupplierMaterPo.setOrdSupId(suppCusPo.getSupId());

				orderSupplierMaterPo.setOrdMaterLevel(orderCustFoodVoList.get(i).getOrdMaterLevel());
				// 插入供货生附表信息
				selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				// }
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(String.valueOf(ordId));
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustPo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustPo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustPo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustPo.getOrdId());
				orderSupplierPo.setOrdState(3);
				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				selectFoodService.addOrderSupplier(orderSupplierPo);

			}

			// 插入到真实需要的表中
			JSONArray arrayReal = JSONArray.fromObject(selectMaterVo.getMaterdategridList());
			// String ordSupId = "";
			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();
			for (int i = 0; i < arrayReal.size(); i++) {
				JSONObject object = JSONObject.fromObject(arrayReal.get(i));
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("materId").toString()));
				// 设定实际重量
				tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(object.get("materNum").toString()));

				// 直接选择材料不设置 需要重量表 ，因为不会自动产生出库订单
				/*
				 * // 需要的重量 tOrderSupplierMaterRealVo .setOrdMaterWeight(new
				 * BigDecimal(object.get("materNum").toString()));
				 */

				tOrderSupplierMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(Integer.valueOf(object.get("supId").toString()));
				// ordSupId = object.get("supId").toString();
				// 订单id
				tOrderSupplierMaterRealVo.setOrdId(ordId);
				tOrderSupplierMaterRealVo.setOrdCustId((int) shopId);
				tOrderSupplierMaterRealVo.setCheckflag(true);
				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);

			}

			// 插入到材料真实进货表中
			boolean flag = selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList);
			/*
			 * if (flag) { OrderSupplierPo orderSupplierPo = new
			 * OrderSupplierPo(); orderSupplierPo =
			 * supplierOrderService.getOrderSupByOrdIdSupId(ordId, ordSupId);
			 * supplierOrderService.updateOrderSupPoState(orderSupplierPo, 3);
			 * json1.put("success", true); } else { json1.put("success", false);
			 * json1.put("msg", selectFoodService.getMsg()); return
			 * json1.toString(); }
			 */

			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/findMyOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMyOrder(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, OrderCustVo orderCustVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		orderCustVo.setOrdCustId((int) shopId);
		this.pageList = selectFoodService.listMyOrder(pageController, orderCustVo);
		List<OrderCustVo> orderCustList = new ArrayList<OrderCustVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderCustVo f = new OrderCustVo();
				f.setOrdId((Integer) item[0]);
				f.setOrdCustId((Integer) item[1]);
				f.setOrdState((Integer) item[2]);
				f.setOrdDate(item[3].toString());
				f.setOrdDay(item[4].toString());
				if (item[5] != null && !item[5].equals("")) {
					f.setOrdValue(new BigDecimal(item[5].toString()));
				}
				f.setReviewRemark((String) item[6]);
				f.setOrdCustName((String) item[7]);
				f.setSubmitOrdTimeLast((String) item[8]);
				f.setBuySelfFlag((Integer) item[9]);

				f.setContent("查看订单详细内容");
				orderCustList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderCustList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/reviewOrderList.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewOrderList(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, OrderCustVo orderCustVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		UserPo userInfo = (UserPo) session.getAttribute("user");
		UserLevelPo tUserLevelPo = userService.getUserLevelPo(userInfo.getUserId());
		orderCustVo.setCompanyUser(tUserLevelPo.getCompanyUser());
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = selectFoodService.listAllOrderForpurchase(pageController, orderCustVo);
		List<OrderCustVo> orderCustList = new ArrayList<OrderCustVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderCustVo f = new OrderCustVo();
				f.setOrdId((Integer) item[0]);
				f.setOrdCustId((Integer) item[1]);
				f.setOrdState((Integer) item[2]);
				f.setOrdDate(item[3].toString());
				f.setOrdDay(item[4].toString());
				if (item[5] != null && !item[5].equals("")) {
					f.setOrdValue(new BigDecimal(item[5].toString()));
				}
				f.setOrdCustName(item[6].toString());
				f.setReviewRemark((String) item[7]);
				f.setSubmitOrdTimeLast((String) item[8]);
				f.setBuySelfFlag((Integer) item[9]);
				f.setContent("查看订单详细内容");

				orderCustList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderCustList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/passOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String passOrder(String ordId) {
		JSONObject json = new JSONObject();
		try {
			OrderCustVo orderCustVo = new OrderCustVo();
			// 取得订单主表实体对象
			orderCustVo = selectFoodService.getOrderCust(ordId);
			selectFoodService.passOrder(orderCustVo);
			// 取得订单材料表实体对象
			List<OrderCustFoodVo> orderCustFoodVoList = null;
			orderCustFoodVoList = selectFoodService.getOrderCustFood(ordId);
			for (int i = 0; i < orderCustFoodVoList.size(); i++) {
				// 取得菜品材料对应表
				List<FoodMaterPo> foodMaterPoList = selectFoodService.getFootMaterByFoodId(orderCustFoodVoList.get(i).getOrdFoodId());
				for (int j = 0; j < foodMaterPoList.size(); j++) {

					OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
					orderSupplierMaterPo.setOrdId(Integer.valueOf(ordId));
					orderSupplierMaterPo.setOrdFoodId(orderCustFoodVoList.get(i).getOrdFoodId());
					orderSupplierMaterPo.setOrdMaterId(foodMaterPoList.get(j).getMaterId());

					// 食材 价格 重量信息
					BigDecimal weightNum = new BigDecimal(orderCustFoodVoList.get(i).getOrdFoodNum());
					BigDecimal weightSel = foodMaterPoList.get(j).getMaterWeight();
					BigDecimal weight = weightSel.multiply(weightNum);

					MaterPo materPo = selectFoodService.getMaterValueByMaterId(foodMaterPoList.get(j).getMaterId());

					MaterLevelPo materLevelPo = selectFoodService.getMaterLevelByMaterId(foodMaterPoList.get(j).getMaterId());
					// 设置需要采购的实际重量

					BigDecimal weightLastIn = weight.multiply(new BigDecimal(materLevelPo.getMaterProportion())).multiply(new BigDecimal("0.01"))
							.multiply(new BigDecimal("0.001"));
					// 计算后的公斤数 舍去小数点 如果小数点后有值则进一位
					weightLastIn = weightLastIn.setScale(0, BigDecimal.ROUND_UP);

					orderSupplierMaterPo.setOrdMaterWeight(weightLastIn);

					// 设置需要采购的的实际价钱
					BigDecimal value = materLevelPo.getMaterValueUndressed().multiply(weightLastIn).multiply(new BigDecimal("1000"));
					orderSupplierMaterPo.setOrdMaterValue(value);

					// 供货商
					ShopSupplierTypePo suppCusPo = selectFoodService.getSuppCusPoByMaterType(materPo.getMaterType(), orderCustVo.getOrdCustId());
					orderSupplierMaterPo.setOrdSupId(suppCusPo.getSupId());

					orderSupplierMaterPo.setOrdMaterLevel(1);
					// 插入供货生附表信息
					selectFoodService.addOrderSupplierMater(orderSupplierMaterPo);
				}
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(ordId);
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo.setOrdCustId(orderCustVo.getOrdCustId());
				orderSupplierPo.setOrdDate(orderCustVo.getOrdDate());
				orderSupplierPo.setOrdDay(orderCustVo.getOrdDay());
				orderSupplierPo.setOrdId(orderCustVo.getOrdId());
				orderSupplierPo.setOrdState(1);
				Object[] a = list.get(i);
				orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
				orderSupplierPo.setOrdValue(new BigDecimal(a[1].toString()));
				selectFoodService.addOrderSupplier(orderSupplierPo);

			}

			// 生成供应商订单
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/passOrderValueForPurchase.do", method = { RequestMethod.POST })
	@ResponseBody
	public String passOrderValueForPurchase(OrderSupplierVo orderSupplierVo) {
		JSONObject json = new JSONObject();
		try {
			// 取得订单主表实体对象
			selectFoodService.passOrderValueForPurchase(orderSupplierVo);
			// 生成供应商订单
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/nopassOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String nopassOrder(String ordId, String reviewRemark) {
		JSONObject json = new JSONObject();
		try {
			OrderCustVo orderCustVo = new OrderCustVo();
			// 取得订单主表实体对象
			orderCustVo = selectFoodService.getOrderCust(ordId);
			orderCustVo.setReviewRemark(reviewRemark);
			selectFoodService.nopassOrder(orderCustVo);
			// 生成供应商订单
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findcustMaterOrder.do")
	@ResponseBody
	public String findSuppOrder(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierVo orderSupplierVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		orderSupplierVo.setOrdCustId((int) shopId);
		this.pageList = custMaterOrderService.listCustOrder(pageController, orderSupplierVo);
		List<OrderSupplierVo> orderSupplierVoList = new ArrayList<OrderSupplierVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierVo f = new OrderSupplierVo();

				f.setOid((Integer) item[0]);
				f.setOrdId((Integer) item[1]);
				f.setOrdCustId((Integer) item[2]);
				f.setOrdSupId((Integer) item[3]);
				f.setOrdState((Integer) item[4]);
				f.setOrdDate((String) item[5]);
				f.setOrdDay((String) item[6]);
				f.setOrdValue((BigDecimal) item[9]);
				f.setOrdCustName((String) item[10]);
				f.setOrdSupName((String) item[11]);
				f.setOrdPaymentState((Integer) item[12]);
				f.setOrdValueReal((BigDecimal) item[13]);
				f.setCommitDeliverState((Integer) item[14]);
				f.setContent("查看详细清单");
				orderSupplierVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findcustMaterOrderAll.do")
	@ResponseBody
	public String findcustMaterOrderAll(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierVo orderSupplierVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		UserLevelPo tUserLevelPo = userService.getUserLevelPo(userInfo.getUserId());
		orderSupplierVo.setCompanyUser(tUserLevelPo.getCompanyUser());
		this.pageList = custMaterOrderService.listCustOrderAllForpurchase(pageController, orderSupplierVo);
		List<OrderSupplierVo> orderSupplierVoList = new ArrayList<OrderSupplierVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierVo f = new OrderSupplierVo();

				f.setOid((Integer) item[0]);
				f.setOrdId((Integer) item[1]);
				f.setOrdCustId((Integer) item[2]);
				f.setOrdSupId((Integer) item[3]);
				f.setOrdState((Integer) item[4]);
				f.setOrdDate((String) item[5]);
				f.setOrdDay((String) item[6]);
				f.setOrdValue((BigDecimal) item[9]);
				f.setOrdCustName((String) item[10]);
				f.setOrdSupName((String) item[11]);
				f.setOrdPaymentState((Integer) item[12]);
				f.setOrdValueReal((BigDecimal) item[13]);
				f.setCommitDeliverState((Integer) item[14]);

				f.setContent("查看详细清单");
				orderSupplierVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 收货control
	 */
	@RequestMapping(value = "/delivery.do", method = { RequestMethod.POST })
	@ResponseBody
	public String delivery(String ordId, String ordCustId, String ordSupId, String ordDate, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
			orderSupplierPo = custMaterOrderService.getOrderSup(ordId, ordCustId, ordSupId);
			custMaterOrderService.sendmater(orderSupplierPo);

			// 入库
			storeService.addStoreMaterIn(ordId, ordCustId, ordSupId, ordDate);

			// List list =
			// supplierOrderService.getOrderSupMaterRealForInStore(ordId,
			// ordCustId, ordSupId);

			//
			// for (int i = 0; i < list.size(); i++) {
			// Object[] item = (Object[]) list.get(i);
			// StoreMaterVo tStoreMaterVo = new StoreMaterVo();
			// tStoreMaterVo.setMaterId((Integer) item[0]);
			// tStoreMaterVo.setMaterNum(new BigDecimal(item[1].toString()));
			// tStoreMaterVo.setMaterLevel((Integer) item[2]);
			// storeMaterVoList.add(tStoreMaterVo);
			// }
			//
			// // 生成出库订单
			// UserPo userInfo = (UserPo) session.getAttribute("user");
			// StoreOutVo storeOutVo = new StoreOutVo();
			// storeOutVo.setCreatDate(ordDate);
			// // storeOutVo.setRemark(storeMaterVo.getRemark());
			//
			// int storeHouseId = storeService.getStoreHouseId(ordCustId);
			// storeOutVo.setStoreHouseId(storeHouseId);
			// storeOutVo.setStoreOutState(1);// 状态未确认
			// storeOutVo.setStoreOutWay(2);// 自动动入库
			//
			// int storeOutId = storeService.minStoreOut(storeOutVo, userInfo);
			//
			// storeService.minStoreOutDetailed(storeMaterVoList, storeOutId);
			//
			// // 生成供应商订单
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	/**
	 * 进入客户详细订单 for cust
	 * */
	@RequestMapping(value = "/goCustomDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goCustomDetailed(String id, String supId, HttpSession session) {
		String mobile = session.getAttribute("mobile").toString();

		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);

		if (mobile.equals("open")) {
			return new ModelAndView("/jsp-mobile/ordermanage/customDetailedMaterIndex_moblie", map);
		} else {
			return new ModelAndView("/ordermanage/customDetailedMaterIndex", map);
		}
	}

	/**
	 * 进入客户详细订单 for admin
	 * */
	@RequestMapping(value = "/goCustomDetailedForAdmin.do", method = { RequestMethod.GET })
	public ModelAndView goCustomDetailedForAdmin(String id, String supId) {
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);
		return new ModelAndView("/ordermanage/customDetailedMaterIndexForAdmin", map);
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findcustMaterDetailed.do")
	@ResponseBody
	public String findCustDetailOrder(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierMaterVo orderSupplierMaterVo, HttpSession session, String supId) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";

		// 设置rows 长度
		List list = custMaterDetailedOrderService.rowslistCustOrderDetailed(String.valueOf(orderSupplierMaterVo.getOid()), supId);
		if (list != null && list.size() > 0) {
			BigInteger rowObject = (BigInteger) list.get(0);
			rows = rowObject.toString();
		} else if (rows == null || rows.equals("")) {
			rows = "10";
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = custMaterDetailedOrderService.listCustOrderDetailed(pageController, orderSupplierMaterVo,
				String.valueOf(orderSupplierMaterVo.getOid()), supId);
		List<OrderSupplierMaterVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterVo>();
		List items = pageList.getItems();

		BigDecimal ordValueRealAll = new BigDecimal(0);

		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterVo f = new OrderSupplierMaterVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdSupId(Integer.valueOf(item[1].toString()));
				f.setOrdMaterId(Integer.valueOf(item[2].toString()));
				f.setOrdMaterWeight(new BigDecimal(item[3].toString()));
				f.setOrdMaterValue(new BigDecimal(item[4].toString()));
				f.setOrdMaterName(item[6].toString());

				f.setOrdCustId(Integer.valueOf(item[7].toString()));
				f.setOrdCustName(item[8].toString());
				f.setOrdSupName(item[9].toString());
				f.setOrdSupMaterId((Integer) item[10]);
				f.setOrdMaterWeightReal(new BigDecimal(item[11].toString()));
				f.setOrdMaterValueReal(new BigDecimal(item[12].toString()));

				ordValueRealAll = ordValueRealAll.add(f.getOrdMaterValueReal());
				/*
				 * if (item[13] == null) { f.setStoreWeight(new BigDecimal(0));
				 * } else { f.setStoreWeight(new
				 * BigDecimal(item[13].toString())); }
				 */
				// 品级的设置
				if (item[5] != null && !item[5].equals("")) {
					f.setOrdMaterLevel((Integer) item[5]);
				} else {
					f.setOrdMaterLevel(1);
				}
				String levelname = "";
				if (f.getOrdMaterLevel() == 1) {
					levelname = "优";
				} else if (f.getOrdMaterLevel() == 2) {
					levelname = "中";
				} else if (f.getOrdMaterLevel() == 3) {
					levelname = "差";
				}
				f.setLevelname(levelname);

				f.setContent("修改菜品品级");

				f.setUnit((String) item[13]);
				f.setSpecifications((String) item[14]);
				f.setBrands((String) item[15]);
				// f.setUnusualRemark((String) item[17]);
				f.setReturnGoods((Integer) item[16]);
				f.setSubmitOrdTime((String) item[17]);

				if (f.getOrdMaterWeightReal().compareTo(new BigDecimal(0)) != 0) {
					f.setOrdMaterValueRealEvery(f.getOrdMaterValueReal().divide(f.getOrdMaterWeightReal(), 2, BigDecimal.ROUND_HALF_UP));
				} else {
					f.setOrdMaterValueRealEvery(new BigDecimal(0));
				}
				f.setOrdMaterValueRealUnit(new BigDecimal(item[18].toString()));
				f.setOrdMaterValueRealCost(new BigDecimal(item[19].toString()));
				f.setOrdMaterValueRealCostUnit(new BigDecimal(item[20].toString()));
				f.setLeaveMessage((String) item[21]);
				f.setMaterType((String) item[22]);
				f.setOrdDate((String) item[23]);
				f.setPeriodValueFlag((String) item[24]);

				orderSupplierMaterVoList.add(f);
			}
		}

		List<OrderSupplierMaterVoFooter> orderSupplierVoListFooter = new ArrayList<OrderSupplierMaterVoFooter>();
		OrderSupplierMaterVoFooter footer = new OrderSupplierMaterVoFooter();
		footer.setOrdCustName("合计");
		footer.setOrdMaterValueReal(ordValueRealAll);
		orderSupplierVoListFooter.add(footer);
		json.put("footer", orderSupplierVoListFooter);

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		return json.toString();
	}

	/**
	 * 进入供货商详细订单
	 * */
	@RequestMapping(value = "/goSupperDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goSupperDetailed(String id, String supId) {
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);
		try {
			// 取得订单状态
			OrderSupplierPo tOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(id, supId).get(0);
			map.put("ordState", tOrderSupplierPo.getOrdState());
			map.put("tOrderSupplierPo", tOrderSupplierPo);

			ShopPo shopPo = shopService.getShopPoByOrdId(Long.valueOf(id));
			map.put("shopPo", shopPo);
			boolean foodOrMaterHtmlFalg = selectFoodService.judgeFoodOrMaterHtml(id);

			if (foodOrMaterHtmlFalg) {
				return new ModelAndView("/supplier/supperDetailedIndex", map);
			} else {
				return new ModelAndView("/ordermanage/customDetailedMaterIndexForSup", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/base/error", map);
		}
	}

	/**
	 * 进入供货商详细订单
	 * */
	@RequestMapping(value = "/goSupperDetailedForMobile.do", method = { RequestMethod.GET })
	public ModelAndView goSupperDetailedForMobile(String id, String supId) {
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);
		try {
			// 取得订单状态
			OrderSupplierPo tOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(id, supId).get(0);
			map.put("ordState", tOrderSupplierPo.getOrdState());
			map.put("tOrderSupplierPo", tOrderSupplierPo);

			ShopPo shopPo = shopService.getShopPoByOrdId(Long.valueOf(id));
			map.put("shopPo", shopPo);
			map.put("shopName", shopPo.getShopName());
			// return new
			// ModelAndView("redirect:/jsp-mobile/ordermanage/customDetailedMaterIndexForSup_mobile.jsp",
			// map);
			return new ModelAndView("/jsp-mobile/ordermanage/customDetailedMaterIndexForSup_mobile", map);

		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/base/error", map);
		}
	}

	/**
	 * 进入供货商详细订单
	 * */
	@RequestMapping(value = "/goSupperDetailedRealSend.do", method = { RequestMethod.GET })
	public ModelAndView goSupperDetailedRealSend(String id) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		map.put("ordId", id);
		return new ModelAndView("/supplier/supperDetailedIndexRealSend", map);
	}

	/**
	 * 查看品级
	 * */
	@RequestMapping(value = "/lookupLevel.do", method = { RequestMethod.GET })
	public ModelAndView lookupLevel(String supId, String materId) {
		JSONObject json = new JSONObject();

		Map map = new HashMap();
		map.put("supId", supId);
		map.put("materId", materId);

		try {

			return new ModelAndView("/supplier/lookupLevel", map);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
			return new ModelAndView("/base/error", map);
		}
	}

	/**
	 * 客户审核页面的详细内容
	 * */
	@RequestMapping(value = "/goSelectCustomDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goSelectCustomDetailed(String id) {
		JSONObject json = new JSONObject();

		Map map = new HashMap();
		map.put("ordId", id);
		try {
			boolean foodOrMaterHtmlFalg = selectFoodService.judgeFoodOrMaterHtml(id);

			if (foodOrMaterHtmlFalg) {
				return new ModelAndView("/ordermanage/selectFoodDetailedIndex", map);
			} else {
				return new ModelAndView("/ordermanage/selectMaterDetailedIndex", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
			return new ModelAndView("/base/error", map);
		}
	}

	/**
	 * 客户审核页面的详细内容
	 * */
	@RequestMapping(value = "/goSelectCustomDetailedForAdmin.do", method = { RequestMethod.GET })
	public ModelAndView goSelectCustomDetailedForAdmin(String id) {
		JSONObject json = new JSONObject();

		Map map = new HashMap();
		map.put("ordId", id);
		try {
			boolean foodOrMaterHtmlFalg = selectFoodService.judgeFoodOrMaterHtml(id);

			if (foodOrMaterHtmlFalg) {
				return new ModelAndView("/ordermanage/selectFoodDetailedIndex", map);
			} else {
				return new ModelAndView("/ordermanage/selectMaterDetailedIndexForAdmin", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
			return new ModelAndView("/base/error", map);
		}
	}

	@RequestMapping(value = "/deleteOrderSupperMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteOrderSupperMater(String ordId, String ordMaterId, String ordSupId) {
		JSONObject json = new JSONObject();
		try {
			// 判断订单是否发货
			boolean flag = selectFoodService.getOrderSupplierPoByOrdIdAndSupIdForCheck(String.valueOf(ordId), ordSupId);
			if (!flag) {
				json.put("success", false);
				json.put("msg", "删除失败，已经有订单发货，请查看供货商订单联系退货！");
				return json.toString();
			}

			// 微信提醒start
			List<OrderSupplierPo> listOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(ordId, ordSupId);
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderForUpdate("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}
			}
			// 微信提醒 over

			custMaterOrderService.deleteOrderSupperMater(ordId, ordMaterId, ordSupId);

			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteOrderSupperMaterList.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteOrderSupperMaterList(String jsonString) {
		JSONObject json = new JSONObject();
		try {

			JSONArray array = JSONArray.fromObject(jsonString);

			JSONObject objectCheck = JSONObject.fromObject(array.get(0));
			boolean flag = selectFoodService.getOrderSupplierPoByOrdIdAndSupIdForCheck(objectCheck.get("ordId").toString(),
					objectCheck.get("ordSupId").toString());
			if (!flag) {
				json.put("success", false);
				json.put("msg", "删除失败，订单已发货，请查看供货商订单联系退货！");
				return json.toString();
			}

			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				custMaterOrderService.deleteOrderSupperMater(object.get("ordId").toString(), object.get("ordMaterId").toString(),
						object.get("ordSupId").toString());
			}
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findSupMaterDetailed.do")
	@ResponseBody
	public String findSupMaterDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierMaterVo orderSupplierMaterVo, HttpSession session, String oid) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		// 根据用户id得出supid
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderSupplierMaterVo.setOrdSupId(supId);

		this.pageList = custMaterDetailedOrderService.listSupperOrderDetailed(pageController, orderSupplierMaterVo, oid);
		List<OrderSupplierMaterVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterVo f = new OrderSupplierMaterVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdSupId(Integer.valueOf(item[1].toString()));
				f.setOrdMaterId(Integer.valueOf(item[2].toString()));
				f.setOrdMaterWeight(new BigDecimal(item[3].toString()));
				f.setOrdMaterValue(new BigDecimal(item[4].toString()));
				f.setOrdMaterName(item[6].toString());

				f.setOrdCustId(Integer.valueOf(item[7].toString()));
				f.setOrdCustName(item[8].toString());

				f.setOrdMaterWeightReal((BigDecimal) item[9]);
				f.setOrdMaterValueReal(new BigDecimal(item[10].toString()));
				f.setOrdSupName(item[11].toString());

				f.setOrdMaterLevel((Integer) item[5]);

				List<MaterLevelPo> list = supplierService.getMaterLevelPoByMaterIdSupId(Integer.valueOf(item[2].toString()), supId);

				String a = "<select class=\"easyui-combobox\" id='" + item[2].toString() + "select" + "' >";
				for (int j = 0; j < list.size(); j++) {
					if (list.get(j).getMaterLevel() == 1 && (Integer) item[5] == 1) {
						a = a + "<option selected = 'selected' value='" + list.get(j).getMaterLevel() + "'>优</option> ";
					} else if (list.get(j).getMaterLevel() == 2 && (Integer) item[5] == 2) {
						a = a + "<option selected = 'selected' value='" + list.get(j).getMaterLevel() + "'>中</option> ";
					} else if (list.get(j).getMaterLevel() == 3 && (Integer) item[5] == 3) {
						a = a + "<option selected = 'selected' value='" + list.get(j).getMaterLevel() + "'>差</option> ";
					} else if (list.get(j).getMaterLevel() == 1 && (Integer) item[5] != 1) {
						a = a + "<option  value='" + list.get(j).getMaterLevel() + "'>优</option> ";
					} else if (list.get(j).getMaterLevel() == 2 && (Integer) item[5] != 2) {
						a = a + "<option  value='" + list.get(j).getMaterLevel() + "'>中</option> ";
					} else if (list.get(j).getMaterLevel() == 3 && (Integer) item[5] != 3) {
						a = a + "<option  value='" + list.get(j).getMaterLevel() + "'>差</option> ";
					}

				}
				a = a + "</select>";

				f.setOrdMaterLevelSelect(a);

				// 品级的设置
				/*
				 * if (item[5] != null && !item[5].equals("")) {
				 * f.setOrdMaterLevel((Integer) item[5]); } else {
				 * f.setOrdMaterLevel(1); } String levelname = ""; if
				 * (f.getOrdMaterLevel() == 1) { levelname = "优"; } else if
				 * (f.getOrdMaterLevel() == 2) { levelname = "中"; } else if
				 * (f.getOrdMaterLevel() == 3) { levelname = "差"; }
				 * f.setLevelname(levelname);
				 */

				f.setContent("可选品级");
				orderSupplierMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findSelectMaterDetailed.do")
	@ResponseBody
	public String findSelectMaterDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierMaterVo orderSupplierMaterVo, HttpSession session, String oid) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		/*
		 * UserPo userInfo = (UserPo) session.getAttribute("user");
		 * 
		 * // 根据用户id得出supid int supId =
		 * supplierService.getSupIdByUserId(userInfo.getUserId());
		 * orderSupplierMaterVo.setOrdSupId(supId);
		 */

		this.pageList = custMaterDetailedOrderService.listSelectMaterDetailed(pageController, orderSupplierMaterVo, oid);
		List<OrderSupplierMaterVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterVo f = new OrderSupplierMaterVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdMaterId(Integer.valueOf(item[1].toString()));
				f.setOrdMaterName(item[2].toString());
				f.setOrdMaterWeight(new BigDecimal(item[3].toString()));
				f.setOrdMaterValue(new BigDecimal(item[4].toString()));
				// 品级的设置
				f.setOrdMaterLevel((Integer) item[5]);
				String levelname = "";
				if (f.getOrdMaterLevel() == 1) {
					levelname = "优";
				} else if (f.getOrdMaterLevel() == 2) {
					levelname = "中";
				} else if (f.getOrdMaterLevel() == 3) {
					levelname = "差";
				}
				f.setLevelname(levelname);

				f.setOrdCustId(Integer.valueOf(item[6].toString()));
				f.setOrdCustName(item[7].toString());
				f.setOrdSupId(Integer.valueOf(item[8].toString()));
				f.setOrdSupName(item[9].toString());
				f.setOrdSupMaterId((Integer) item[10]);

				f.setUnit((String) item[11]);
				f.setSpecifications((String) item[12]);
				f.setBrands((String) item[13]);
				f.setSubmitOrdTime((String) item[14]);
				f.setLeaveMessage((String) item[15]);
				f.setMaterType(item[16].toString());
				orderSupplierMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findMatertoMaterDetailed.do")
	@ResponseBody
	public String findMatertoMaterDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierMaterVo orderSupplierMaterVo, HttpSession session, String oid) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");

		// 根据用户id得出supid
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderSupplierMaterVo.setOrdSupId(supId);

		this.pageList = custMaterDetailedOrderService.listMaterToMaterDetailed(pageController, orderSupplierMaterVo, oid);
		List<OrderSupplierMaterVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterVo f = new OrderSupplierMaterVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdMaterId(Integer.valueOf(item[1].toString()));
				f.setOrdMaterName(item[2].toString());
				f.setOrdMaterWeight(new BigDecimal(item[3].toString()));
				f.setOrdMaterValue(new BigDecimal(item[4].toString()));
				// 品级的设置
				f.setOrdMaterLevel((Integer) item[5]);
				String levelname = "";
				if (f.getOrdMaterLevel() == 1) {
					levelname = "优";
				} else if (f.getOrdMaterLevel() == 2) {
					levelname = "中";
				} else if (f.getOrdMaterLevel() == 3) {
					levelname = "差";
				}
				f.setLevelname(levelname);

				f.setOrdCustId(Integer.valueOf(item[6].toString()));
				f.setOrdCustName(item[7].toString());
				f.setOrdSupId(Integer.valueOf(item[8].toString()));
				f.setOrdSupName(item[9].toString());

				orderSupplierMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 供货商查看实际发货重量
	 */
	@RequestMapping(value = "/findMatertoMaterDetailedReal.do")
	@ResponseBody
	public String findMatertoMaterDetailedReal(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderSupplierMaterVo orderSupplierMaterVo, HttpSession session, String oid) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");

		// 根据用户id得出supid
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderSupplierMaterVo.setOrdSupId(supId);

		this.pageList = custMaterDetailedOrderService.findMatertoMaterDetailedReal(pageController, orderSupplierMaterVo, oid);
		List<OrderSupplierMaterVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterVo f = new OrderSupplierMaterVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdMaterId(Integer.valueOf(item[1].toString()));
				f.setOrdMaterName(item[2].toString());
				f.setOrdMaterWeight(new BigDecimal(item[3].toString()));
				f.setOrdMaterValue((BigDecimal) item[4]);
				// 品级的设置
				f.setOrdMaterLevel((Integer) item[5]);
				String levelname = "";
				if (f.getOrdMaterLevel() == 1) {
					levelname = "优";
				} else if (f.getOrdMaterLevel() == 2) {
					levelname = "中";
				} else if (f.getOrdMaterLevel() == 3) {
					levelname = "差";
				}
				f.setLevelname(levelname);

				f.setOrdCustId(Integer.valueOf(item[6].toString()));
				f.setOrdCustName(item[7].toString());
				f.setOrdSupId(Integer.valueOf(item[8].toString()));
				f.setOrdSupName(item[9].toString());

				orderSupplierMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findSelectFoodDetailed.do")
	@ResponseBody
	public String findSelectFoodDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderCustFoodVo orderCustFoodVo, HttpSession session, String oid) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		// orderCustFoodVo.setOrdSupId((int) userInfo.getUserId());
		this.pageList = custMaterDetailedOrderService.listSelectFoodDetailed(pageController, orderCustFoodVo, oid);
		List<OrderCustFoodVo> orderCustFoodVoList = new ArrayList<OrderCustFoodVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderCustFoodVo f = new OrderCustFoodVo();

				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdFoodId(Integer.valueOf(item[1].toString()));
				f.setOrdFoodNum(Integer.valueOf(item[2].toString()));
				f.setOrdFoodValue(new BigDecimal(item[3].toString()));
				f.setOrdFoodName((String) item[4].toString());

				// f.setOrdSupId(Integer.valueOf(item[1].toString()));
				// f.setOrdMaterId(Integer.valueOf(item[2].toString()));
				// f.setOrdMaterWeight(Integer.valueOf(item[3].toString()));
				// f.setOrdMaterValue(new BigDecimal(item[4].toString()));
				orderCustFoodVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderCustFoodVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findMaterLevel.do")
	@ResponseBody
	public String findMaterLevel(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, HttpSession session)
			throws GenericException {
		JSONObject json = new JSONObject();

		// json格式

		String a = "[{\"ordMaterLevel\":\"1\",\"levelname\":\"优\"},{\"ordMaterLevel\":\"2\",\"levelname\":\"中\"},{\"ordMaterLevel\":\"3\",\"levelname\":\"差\"}]";

		return a;
	}

	@RequestMapping(value = "/updateOrderLevel.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateOrderLevel(String ordId, String ordMaterId, int ordMaterLevel, String ordSupId) {
		JSONObject json = new JSONObject();
		try {

			// -----------------------------------------------start
			// OrderCustVo orderCustVo = new OrderCustVo();
			// 取得订单主表实体对象
			// orderCustVo = selectFoodService.getOrderCust(ordId);
			// selectFoodService.passOrder(orderCustVo); //更改订单状态
			// 取得订单材料表实体对象
			List<OrderCustFoodVo> orderCustFoodVoList = null;
			orderCustFoodVoList = selectFoodService.getOrderCustFood(ordId);
			for (int i = 0; i < orderCustFoodVoList.size(); i++) {
				// 取得菜品材料对应表
				List<FoodMaterPo> foodMaterPoList = selectFoodService.getFootMaterByFoodId(orderCustFoodVoList.get(i).getOrdFoodId());
				for (int j = 0; j < foodMaterPoList.size(); j++) {

					if (foodMaterPoList.get(j).getMaterId() == Integer.valueOf(ordMaterId)) {

						OrderSupplierMaterPo orderSupplierMaterPo = new OrderSupplierMaterPo();
						// 根据食物id 和材料id 取得现有的采购 清单数据
						orderSupplierMaterPo = selectFoodService
								.getOrderSupplierMaterPoByFoodIdAndMateridAndOid(String.valueOf(orderCustFoodVoList.get(i).getOrdFoodId()),
										String.valueOf(foodMaterPoList.get(j).getMaterId()), ordId).get(0);

						// 重新修改现有的清单
						// 食材 价格 重量信息
						BigDecimal weightNum = new BigDecimal(orderCustFoodVoList.get(i).getOrdFoodNum());
						BigDecimal weightSel = foodMaterPoList.get(j).getMaterWeight();
						BigDecimal weight = weightSel.multiply(weightNum);

						MaterPo materPo = selectFoodService.getMaterValueByMaterId(foodMaterPoList.get(j).getMaterId());

						MaterLevelPo materLevelPo = selectFoodService.getMaterLevelByMaterIdAndLevel(foodMaterPoList.get(j).getMaterId(),
								ordMaterLevel);
						// 设置需要采购的实际重量
						BigDecimal weightLastIn = weight.multiply(new BigDecimal(materLevelPo.getMaterProportion())).multiply(new BigDecimal("0.01"))
								.multiply(new BigDecimal("0.001"));
						// 计算后的公斤数 舍去小数点 如果小数点后有值则进一位
						weightLastIn = weightLastIn.setScale(0, BigDecimal.ROUND_UP);

						orderSupplierMaterPo.setOrdMaterWeight(weightLastIn);

						// 设置需要采购的的实际价钱
						BigDecimal value = materLevelPo.getMaterValueUndressed().multiply(weightLastIn).multiply(new BigDecimal("1000"));
						orderSupplierMaterPo.setOrdMaterValue(value);
						// 供货商
						/*
						 * SuppCusPo suppCusPo =
						 * selectFoodService.getSuppCusPoByMaterType
						 * (materPo.getMaterType(), orderCustVo.getOrdCustId());
						 */
						// orderSupplierMaterPo.setOrdSupId(suppCusPo.getSupId());

						orderSupplierMaterPo.setOrdMaterLevel(Integer.valueOf(ordMaterLevel));
						// 插入供货生附表信息
						selectFoodService.updateOrderSupplierMater(orderSupplierMaterPo);
					}
				}
			}

			// ----生成材料订单的主表---
			List<Object[]> list = selectFoodService.getOrderSupplierMaterByOrdId(ordId);
			for (int i = 0; i < list.size(); i++) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(ordId, ordSupId).get(0);
				/*
				 * orderSupplierPo.setOrdCustId(orderCustVo.getOrdCustId());
				 * orderSupplierPo.setOrdDate(orderCustVo.getOrdDate());
				 * orderSupplierPo.setOrdDay(orderCustVo.getOrdDay());
				 * orderSupplierPo.setOrdId(orderCustVo.getOrdId());
				 */
				Object[] a = list.get(i);

				if (Integer.valueOf(a[0].toString()) == orderSupplierPo.getOrdSupId()) {
					orderSupplierPo.setOrdSupId(Integer.valueOf(a[0].toString()));
					selectFoodService.updateOrderSupplier(orderSupplierPo);
				}
			}
			// -----------------------------------------------end

			// custMaterDetailedOrderService.updateOrderLevel(ordId, ordMaterId,
			// level, ordSupId);
			// 生成供应商订单
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/commitMaterOrderReal.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderReal(OrderSupplierMaterRealVo orderSupplierMaterRealVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {
			// 检验 是否提交过 和订单状态 ----------下

			boolean commitRealWeight = selectFoodService.checkCommitDeliverState(orderSupplierMaterRealVo.getOrdId(),
					orderSupplierMaterRealVo.getOrdSupId());

			if (commitRealWeight) {
				json1.put("success", false);
				json1.put("msg", selectFoodService.getMsg());
				return json1.toString();
			}
			// 检验 是否提交过 和订单状态 ----------上

			String ordId = "";
			String ordSupId = "";

			// 异常标记
			boolean unusualFlag = true;
			// 异常说明

			JSONArray array = JSONArray.fromObject(orderSupplierMaterRealVo.getMaterdategridList());

			List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList = new ArrayList<OrderSupplierMaterRealVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				OrderSupplierMaterRealVo tOrderSupplierMaterRealVo = new OrderSupplierMaterRealVo();
				tOrderSupplierMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("ordMaterId").toString()));
				// 设定实际重量
				if (object.get("ordMaterWeightReal") != null && !object.get("ordMaterWeightReal").equals("")
						&& !object.get("ordMaterWeightReal").equals("undefined")) {
					tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(object.get("ordMaterWeightReal").toString()));
				} else {
					tOrderSupplierMaterRealVo.setOrdMaterWeightReal(new BigDecimal(0));
				}
				// 需要的重量
				tOrderSupplierMaterRealVo.setOrdMaterWeight(new BigDecimal(object.get("ordMaterWeight").toString()));

				tOrderSupplierMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("ordMaterLevel").toString()));
				// 供货商
				tOrderSupplierMaterRealVo.setOrdSupId(Integer.valueOf(object.get("ordSupId").toString()));
				ordSupId = object.get("ordSupId").toString();
				// 订单id
				tOrderSupplierMaterRealVo.setOrdId(Integer.valueOf(object.get("ordId").toString()));
				ordId = object.get("ordId").toString();

				tOrderSupplierMaterRealVo.setOrdCustId(Integer.valueOf(object.get("ordCustId").toString()));

				// 比较送货重量 判断订单是否异常 -Start
				String unusualRemark = object.get("unusualRemark").toString();
				if (!unusualRemark.equals("")) {
					unusualFlag = false;
					tOrderSupplierMaterRealVo.setUnusualRemark(unusualRemark);
				}
				int compare = new BigDecimal(object.get("ordMaterWeight").toString()).compareTo(new BigDecimal(object.get("ordMaterWeightReal")
						.toString()));
				if (compare == -1) {
					unusualFlag = false;// 送货多于预期
					tOrderSupplierMaterRealVo.setUnusualRemark("送货多于预期");
				} else if (compare == 1) {
					unusualFlag = false;// 送货少于预期
					tOrderSupplierMaterRealVo.setUnusualRemark("送货少于预期");
				}
				// 比较送货重量 判断订单是否异常 -End

				orderSupplierMaterRealVoList.add(tOrderSupplierMaterRealVo);

			}
			// 插入到材料需要的进货表中
			/*
			 * boolean flagNeed = selectFoodService.addOrderSupplierMaterNeed(
			 * orderSupplierMaterRealVoList); if (!flagNeed) {
			 * json1.put("success", false); json1.put("msg",
			 * selectFoodService.getMsg()); return json1.toString(); }
			 */

			// 插入到材料真实进货表中
			// boolean flag =
			// selectFoodService.addOrderSupplierMaterReal(orderSupplierMaterRealVoList);

			String ordDate = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(ordId, ordSupId).get(0).getOrdDate();
			boolean flag = selectFoodService.updateOrderSupplierMaterReal(orderSupplierMaterRealVoList, ordDate);

			if (flag) {
				OrderSupplierPo orderSupplierPo = new OrderSupplierPo();
				orderSupplierPo = supplierOrderService.getOrderSupByOrdIdSupId(ordId, ordSupId);
				// 4位 订单状态待收货
				supplierOrderService.updateOrderSupPoStateAndSetValueReal(orderSupplierPo, 4, unusualFlag);
				// 异常处理 -----start
				if (!unusualFlag) {
					unusualService.addOrderSupplierMaterUnusualPo(orderSupplierMaterRealVoList);
				}
				// 异常处理------end

				json1.put("success", true);
			} else {
				json1.put("success", false);
				json1.put("msg", selectFoodService.getMsg());
				return json1.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/findEveryCustOrderState.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findEveryOrderState(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderStateVo orderStateVo, HttpSession session) throws GenericException {

		JSONObject json = new JSONObject();

		if (orderStateVo.getOrderState() == 0) {
			json.put("success", true);
			return json.toString();
		}

		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = selectFoodService.findEveryOrderState(pageController, orderStateVo);
		List<OrderStateVo> orderStateVoList = new ArrayList<OrderStateVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderStateVo f = new OrderStateVo();
				f.setCustId((Integer) item[0]);
				f.setCustName((String) item[1]);
				BigInteger phone = (BigInteger) item[2];
				f.setUserPhone(phone.longValue());
				f.setOrderState(orderStateVo.getOrderState());
				orderStateVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderStateVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findEverySupOrderState.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findEverySupOrderState(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderStateVo orderStateVo, HttpSession session) throws GenericException {

		JSONObject json = new JSONObject();

		if (orderStateVo.getOrderState() == 0) {
			json.put("success", true);
			return json.toString();
		}

		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = selectFoodService.findEveryOrderState(pageController, orderStateVo);
		List<OrderStateVo> orderStateVoList = new ArrayList<OrderStateVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderStateVo f = new OrderStateVo();
				f.setSupId((Integer) item[0]);
				f.setSupName((String) item[1]);
				BigInteger phone = (BigInteger) item[2];
				f.setUserPhone(phone.longValue());
				f.setOrderState(orderStateVo.getOrderState());
				orderStateVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderStateVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/deleteOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteOrder(int ordId, String ordDate, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");

			// 增加对时间的校验
			boolean flagTime = false;
			flagTime = timeControlService.timeControl(userInfo.getUserId(), ordDate);
			if (!flagTime) {
				json.put("success", false);
				json.put("msg", "msg001");
				return json.toString();
			}

			// 校验订单是否等待发货
			boolean checkflag = selectFoodService.getOrderSupplierPoByOrdIdForCheck(String.valueOf(ordId));
			if (!checkflag) {
				json.put("success", false);
				json.put("msg", "删除失败，已经有订单发货，请查看供货商订单联系退货！");
				return json.toString();
			}

			// 微信提醒start
			List<OrderSupplierPo> listOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdId(String.valueOf(ordId));
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderForUpdate("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}
			}
			// 微信提醒 over
			selectFoodService.deleteOrder(ordId);

			json.put("success", true);

		} catch (Exception e) {
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/updateOrderSupperMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateOrderSupperMater(String orderList) {
		JSONObject json = new JSONObject();
		try {
			// 微信提醒
			String ordId_weixin = "";
			String ordSupId_weixin = "";
			String lastOrdSupId_weixin = "";

			JSONArray array = JSONArray.fromObject(orderList);
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				String ordId = object.get("ordId").toString();
				String ordMaterId = object.get("ordMaterId").toString();
				String ordSupId = object.get("ordSupId").toString();
				String ordCustId = object.get("ordCustId").toString();

				custMaterOrderService.updateOrderSupperMater(ordId, ordMaterId, ordSupId, ordCustId);
				// noteService.sendMessageToSup(ordId, ordMaterId, ordSupId,
				// shopService.getShopPo(Long.valueOf(ordCustId)).getShopName());
				// 微信提醒
				ordId_weixin = ordId;
				ordSupId_weixin = ordSupId;
				lastOrdSupId_weixin = object.get("lastOrdSupId").toString();

			}
			// 微信上个供货商提醒
			List<OrderSupplierPo> listOrderSupplierPo1 = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(ordId_weixin, lastOrdSupId_weixin);
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo1) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderForUpdate("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}
			}
			// 客户下单时候的微信提醒
			List<OrderSupplierPo> listOrderSupplierPo = selectFoodService.getOrderSupplierPoByOrdIdAndSupId(ordId_weixin, ordSupId_weixin);
			for (OrderSupplierPo orderSupplierPo : listOrderSupplierPo) {
				List<UserPo> userPoList = userService.getUserForSup(orderSupplierPo.getOrdSupId());
				for (UserPo userPo : userPoList) {
					if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
						weixinService.send_template_message_for_commitMaterOrderList("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
								userPo.getOpenId(), orderSupplierPo);
					}
				}

			}
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/unitMaterChange.do", method = { RequestMethod.POST })
	@ResponseBody
	public String unitMaterChange(int ordId, int materId, int auxiliaryMaterId, int ordCustId, BigDecimal ordMaterWeightReal, HttpSession session,
			int ordMaterLevel) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			custMaterOrderService.unitMaterChange(ordId, materId, auxiliaryMaterId, supId, ordCustId, ordMaterWeightReal, ordMaterLevel);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/unitMaterChangeBatch.do", method = { RequestMethod.POST })
	@ResponseBody
	public String unitMaterChangeBatch(int ordId, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			custMaterOrderService.unitMaterChangeBatch(ordId, supId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/unitChangeBatchAllOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String unitChangeBatchAllOrder(String ordDate, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			List<OrderSupplierPo> list = selectFoodService.getOrderSupplierPoByOrdDateAndSupId(ordDate, supId);
			for (OrderSupplierPo orderSupplierPo : list) {
				custMaterOrderService.unitMaterChangeBatch(orderSupplierPo.getOrdId(), supId);
			}
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

}
